﻿using PhotoKiosk.ViewModel.Helpers;
using PhotoKiosk.ViewModel.UserControls;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel
{
    public class SystemSettingsViewModel : ViewModelBase
    {
        private const int k_Uninitialized = -6;
        private MainWindowViewModel m_mainWindowViewModel;

        public SystemSettingsViewModel(MainWindowViewModel i_mainWindowViewModel)
        {
            m_mainWindowViewModel = i_mainWindowViewModel;
        }

        #region Language
        private List<LanguageButtonViewModel> m_languageButtonViewModels;
        public List<LanguageButtonViewModel> LanguageButtonViewModels
        {
            get
            {
                if (m_languageButtonViewModels == null)
                {
                    m_languageButtonViewModels = new List<LanguageButtonViewModel>();
                    LanguageButtonViewModel dgbvm = new LanguageButtonViewModel(new System.Globalization.CultureInfo("he-IL"));
                    dgbvm.OnClick += onLanguageSelected;
                    m_languageButtonViewModels.Add(dgbvm);
                    LanguageButtonViewModel dgbvm1 = new LanguageButtonViewModel(new System.Globalization.CultureInfo("en-US"));
                    dgbvm.OnClick += onLanguageSelected;
                    m_languageButtonViewModels.Add(dgbvm1);
                    
                }
                return m_languageButtonViewModels;
            }
        }

        private void onLanguageSelected(CultureInfo i_cultureInfo)
        {
            m_mainWindowViewModel.SystemData.CurrentLanguage = i_cultureInfo;
            System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
            Application.Current.Shutdown();

        }
        #endregion

        #region Print Reception
        public bool PrintReception
        {
            get
            {
                return m_mainWindowViewModel.SystemData.PrintReception;
            }
            set
            {
                m_mainWindowViewModel.SystemData.PrintReception = value;
                OnPropertyChanged("PrintReception");
            }
        }
        #endregion

        #region Kiosk Id
        private int m_kioskId = k_Uninitialized;
        public int KioskId
        {
            get
            {
                if (m_kioskId == k_Uninitialized)
                {
                    m_kioskId = m_mainWindowViewModel.SystemData.KioskID;
                }
                return m_kioskId;
            }
            set
            {
                m_kioskId = value;
                m_mainWindowViewModel.SystemData.KioskID = m_kioskId;
                OnPropertyChanged("KioskId");
            }
        }
        #endregion

        #region Paper Sizes
        private List<PhotoKiosk.Model.PaperSize> m_paperSizes;
        public List<PhotoKiosk.Model.PaperSize> PaperSizes
        {
            get
            {
                if (m_paperSizes == null)
                {
                    m_paperSizes = m_mainWindowViewModel.SystemData.PaperSizes;
                }
                return m_paperSizes;
            }
            set
            {
                m_paperSizes = value;
                m_mainWindowViewModel.SystemData.PaperSizes = m_paperSizes;
                OnPropertyChanged("PaperSizes");
            }
        }

        private DelegateCommand m_paperSizesSaveCommand;

        public ICommand PaperSizesSaveCommand
        {
            get
            {
                if (m_paperSizesSaveCommand == null)
                {
                    m_paperSizesSaveCommand = new DelegateCommand(param => this.paperSizesSaveCommandExecute(), param => this.paperSizesSaveCommandCanExecute());
                }
                return m_paperSizesSaveCommand;
            }

        }
        private bool paperSizesSaveCommandCanExecute()
        {
            return true;
        }

        private void paperSizesSaveCommandExecute()
        {
            m_mainWindowViewModel.SystemData.PaperSizes = PaperSizes;
        }
        #endregion

        #region Order miminum price
        private int m_orderMiminumPrice = k_Uninitialized;
        public int OrderMiminumPrice
        {
            get
            {
                if (m_orderMiminumPrice == k_Uninitialized)
                {
                    m_orderMiminumPrice = m_mainWindowViewModel.SystemData.MinimunOrderPrice;
                }
                return m_orderMiminumPrice;
            }
            set
            {
                m_orderMiminumPrice = value;
                m_mainWindowViewModel.SystemData.MinimunOrderPrice = m_orderMiminumPrice;
                OnPropertyChanged("OrderMiminumPrice");
            }
        }
        #endregion

        #region NextOrderId
        private int m_nextOrderId = k_Uninitialized;
        public int NextOrderId
        {
            get
            {
                if (m_nextOrderId == k_Uninitialized)
                {
                    m_nextOrderId = m_mainWindowViewModel.SystemData.NextOrderId;
                }
                return m_nextOrderId;
            }
            set
            {
                m_nextOrderId = value;
                m_mainWindowViewModel.SystemData.NextOrderId = m_nextOrderId;
                OnPropertyChanged("NextOrderId");
            }
        }
        #endregion

        #region SystemCloseCode

        private DelegateCommand m_systemCloseCodeSaveCommand;
        private string m_systemCloseCode = null;
        public string SystemCloseCode
        {
            get
            {
                if (m_systemCloseCode == null)
                {
                    m_systemCloseCode = m_mainWindowViewModel.SystemData.SystemCloseCode;
                }
                return m_systemCloseCode;
            }
            set
            {
                m_systemCloseCode = value;
                OnPropertyChanged("SystemCloseCode");
            }
        }

        public ICommand SystemCloseCodeSaveCommand
        {
            get
            {
                if (m_systemCloseCodeSaveCommand == null)
                {
                    m_systemCloseCodeSaveCommand = new DelegateCommand(param => this.systemCloseCodeSaveCommandExecute(), param => this.systemCloseCodeSaveCommandCanExecute());
                }
                return m_systemCloseCodeSaveCommand;
            }

        }

        private bool systemCloseCodeSaveCommandCanExecute()
        {
            return true;
        }

        private void systemCloseCodeSaveCommandExecute()
        {
            int i = 0;
            if (m_mainWindowViewModel.SystemData.SystemCloseCode != SystemCloseCode && int.TryParse(SystemCloseCode, out i))
            {
                m_mainWindowViewModel.SystemData.SystemCloseCode = SystemCloseCode;
            }
        }
        #endregion

        #region Change Order Location Save
        private DelegateCommand m_changeOrderLocationSaveCommand;

        public string OrderLocationSave
        {
            get
            {
                return m_mainWindowViewModel.SystemData.OrdersLocationSave;
            }
            set
            {

            }
        }

        public ICommand ChangeOrderLocationSaveCommand
        {
            get
            {
                if (m_changeOrderLocationSaveCommand == null)
                {
                    m_changeOrderLocationSaveCommand = new DelegateCommand(param => this.changeOrderLocationSaveCommandExecute(), param => this.changeOrderLocationSaveCommandCanExecute());
                }
                return m_changeOrderLocationSaveCommand;
            }

        }
        private bool changeOrderLocationSaveCommandCanExecute()
        {
            return false;// File.Exists(_MainWindowViewModel.SystemData.SampleImagesPath);
        }

        private void changeOrderLocationSaveCommandExecute()
        {
           // Process.Start(_MainWindowViewModel.SystemData.SampleImagesPath);
            OnPropertyChanged("OrderLocationSave");
        }
        #endregion

        #region Open Sample Photos Location
        private DelegateCommand m_openSamplePhotosLocationCommand;

        public ICommand OpenSamplePhotosLocationCommand
        {
            get
            {
                if (m_openSamplePhotosLocationCommand == null)
                {
                    m_openSamplePhotosLocationCommand = new DelegateCommand(param => this.openSamplePhotosLocationCommandExecute(), param => this.openSamplePhotosLocationCommandCanExecute());
                }
                return m_openSamplePhotosLocationCommand;
            }

        }
        private bool openSamplePhotosLocationCommandCanExecute()
        {
            return Directory.Exists(m_mainWindowViewModel.SystemData.SampleImagesPath);
        }

        private void openSamplePhotosLocationCommandExecute()
        {
            Process.Start(m_mainWindowViewModel.SystemData.SampleImagesPath);
        }
        #endregion

        #region Open Reception Template
        private DelegateCommand m_openReceptionTemplateCommand;
        public ICommand OpenReceptionTemplateCommand
        {
            get
            {
                if (m_openReceptionTemplateCommand == null)
                {
                    m_openReceptionTemplateCommand = new DelegateCommand(param => this.openReceptionTemplateCommandExecute(), param => this.openReceptionTemplateCommandCanExecute());
                }
                return m_openReceptionTemplateCommand;
            }

        }
        private bool openReceptionTemplateCommandCanExecute()
        {
            return File.Exists(m_mainWindowViewModel.SystemData.ReceptionFormatFileLocation);
        }

        private void openReceptionTemplateCommandExecute()
        {
            Process.Start(m_mainWindowViewModel.SystemData.ReceptionFormatFileLocation);
        }
        #endregion

        public List<string> ImageExtension
        {
            get
            {
                return m_mainWindowViewModel.SystemData.ImageExtensions;
            }
            set
            {
             //   m_mainWindowViewModel.SystemData.ImageExtensions = value;
         //       OnPropertyChanged("ImageExtension");
            }
        }

        public List<string> ImageExtensionsAcceptedByPrinter
        {
            get
            {
                return m_mainWindowViewModel.SystemData.ImageExtensionsAcceptedByPrinter;
            }
            set
            {
                m_mainWindowViewModel.SystemData.ImageExtensionsAcceptedByPrinter = value;
                OnPropertyChanged("ImageExtensionsAcceptedByPrinter");
            }
        }

        #region Return Command
        private DelegateCommand m_returnCommand;
        public ICommand returnCommand
        {
            get
            {
                if (m_returnCommand == null)
                {
                    m_returnCommand = new DelegateCommand(param => this.returnCommandExecute());
                }
                return m_returnCommand;
            }
        }

        private void returnCommandExecute()
        {
            m_mainWindowViewModel.CurrentViewModel = new SelectSourceViewModel(m_mainWindowViewModel);
        }

        #endregion
    }
}
